package com.hnucm.c202001020216.ui;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;

import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.databinding.ActivityMainBinding;
import com.hnucm.c202001020216.ui.fragment.ChooseFragment;
import com.hnucm.c202001020216.ui.fragment.MainFragment;
import com.hnucm.c202001020216.ui.fragment.ScienceFragment;
import com.hnucm.c202001020216.ui.fragment.UserFragment;

/**
 * @author starr
 * 主Activity，分管
 * - MainFragment 首页
 * - ChooseFragment 选车模块
 * - ScienceFragment 科普模块
 * - UserFragment 个人中心
 */
public class MainActivity extends AppCompatActivity {

    private static final int MAIN_ID = 1;
    private static final int CHOOSE_ID = 2;
    private static final int SCIENCE_ID = 3;
    private static final int USER_ID = 4;
    ActivityMainBinding mainBinding;
    MainFragment mainFragment = new MainFragment();
    ChooseFragment chooseFragment = new ChooseFragment();
    ScienceFragment scienceFragment = new ScienceFragment();
    UserFragment userFragment = new UserFragment();


    ConstraintLayout mainBtn;
    ConstraintLayout chooseBtn;
    ConstraintLayout scienceBtn;
    ConstraintLayout userBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        /**
         * 初始化控件
         */
        initView();
        mainBtn.setSelected(true);

        /**
         * 添加所有的Fragment
         */
        initFragment();

        /**
         * 获得传入的界面id，替换显示的Fragment
         */
        changeFragment();

        /**
         * 按钮事件监听
         */
        initClickListener();

    }

    private void changeFragment() {
        int currId = getIntent().getIntExtra("id", MAIN_ID);

        switch (currId) {
            case MAIN_ID: {
                getSupportFragmentManager().beginTransaction().show(mainFragment).hide(chooseFragment).hide(scienceFragment).hide(userFragment).commit();
                mainBtn.setSelected(true);
                chooseBtn.setSelected(false);
                scienceBtn.setSelected(false);
                userBtn.setSelected(false);
            }
            break;
            case CHOOSE_ID: {
                getSupportFragmentManager().beginTransaction().hide(mainFragment).show(chooseFragment).hide(scienceFragment).hide(userFragment).commit();
                mainBtn.setSelected(false);
                chooseBtn.setSelected(true);
                scienceBtn.setSelected(false);
                userBtn.setSelected(false);
            }
            break;
            case SCIENCE_ID: {
                getSupportFragmentManager().beginTransaction().hide(mainFragment).hide(chooseFragment).show(scienceFragment).hide(userFragment).commit();
                mainBtn.setSelected(false);
                chooseBtn.setSelected(false);
                scienceBtn.setSelected(true);
                userBtn.setSelected(false);
            }
            break;
            case USER_ID: {
                getSupportFragmentManager().beginTransaction().hide(mainFragment).hide(chooseFragment).hide(scienceFragment).show(userFragment).commit();
                mainBtn.setSelected(false);
                chooseBtn.setSelected(false);
                scienceBtn.setSelected(false);
                userBtn.setSelected(true);
            }
            break;
            default:
                break;
        }
    }


    private void initClickListener() {
        mainBtn.setOnClickListener((view) -> {
            getSupportFragmentManager().beginTransaction().show(mainFragment).hide(chooseFragment).hide(scienceFragment).hide(userFragment).commit();

            mainBtn.setSelected(true);
            chooseBtn.setSelected(false);
            scienceBtn.setSelected(false);
            userBtn.setSelected(false);
        });

        chooseBtn.setOnClickListener((view) -> {
            getSupportFragmentManager().beginTransaction().hide(mainFragment).show(chooseFragment).hide(scienceFragment).hide(userFragment).commit();

            mainBtn.setSelected(false);
            chooseBtn.setSelected(true);
            scienceBtn.setSelected(false);
            userBtn.setSelected(false);
        });

        scienceBtn.setOnClickListener((view) -> {
            getSupportFragmentManager().beginTransaction().hide(mainFragment).hide(chooseFragment).show(scienceFragment).hide(userFragment).commit();
            mainBtn.setSelected(false);
            chooseBtn.setSelected(false);
            scienceBtn.setSelected(true);
            userBtn.setSelected(false);
        });

        userBtn.setOnClickListener((view) -> {
            getSupportFragmentManager().beginTransaction().hide(mainFragment).hide(chooseFragment).hide(scienceFragment).show(userFragment).commit();
            mainBtn.setSelected(false);
            chooseBtn.setSelected(false);
            scienceBtn.setSelected(false);
            userBtn.setSelected(true);
        });
    }

    private void initFragment() {
        getSupportFragmentManager().beginTransaction().add(R.id.constraintLayout1, mainFragment).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.constraintLayout1, chooseFragment).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.constraintLayout1, scienceFragment).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.constraintLayout1, userFragment).commit();

        getSupportFragmentManager().beginTransaction().show(mainFragment).hide(chooseFragment).hide(scienceFragment).hide(userFragment).commit();
        mainBtn.setSelected(true);
        chooseBtn.setSelected(false);
        scienceBtn.setSelected(false);
        userBtn.setSelected(false);
    }

    private void initView() {
        mainBtn = mainBinding.constraintLayout2;
        chooseBtn = mainBinding.constraintLayout;
        scienceBtn = mainBinding.constraintLayout3;
        userBtn = mainBinding.constraintLayout4;


        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
    }
}